package com.ignou.mca.exam;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ignou.mca.beans.ExamBean;
import com.ignou.mca.beans.ExamStatusBean;
import com.ignou.mca.beans.QuestionsBean;
import com.ignou.mca.beans.UserVO;
import com.ignou.mca.datasource.DatabaseOperations;

/**
 * Servlet implementation class UserAcceptance
 */
public class UserAcceptance extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserAcceptance() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		String userStatus = request.getParameter("userstatus");
		String subject = request.getParameter("subject");
		HttpSession session = request.getSession();
		ServletContext ctx = getServletContext();
		UserVO uvo = (UserVO) session.getAttribute("UserVO");
		String userName = uvo.getUserName();
		
		if(userStatus.equalsIgnoreCase("accepted"))
		{
			DatabaseOperations dbOperations = new DatabaseOperations();
			
			String e = request.getParameter("exam");
			int examId = 0;
			if(e!=null)
			{
				examId = Integer.parseInt(e);					
			}
			
			
			ArrayList<QuestionsBean> ques = dbOperations.viewExamPaper(examId);
			ExamBean examDetails = dbOperations.getExamDetails(examId);
			session.setAttribute("ExamQuestions", ques);
			session.setAttribute("ExamDetails", examDetails);
			ctx.getRequestDispatcher("/UserExamPaper.jsp").forward(request, response);
			
		}
		else
		{
			session.setAttribute("ErrorMessage", "Unable to to contact server at this time. <br>Please try again after sometime.");
			response.sendRedirect("./ErrorDisplay.jsp");
		}
	}

}
